package com.ruoyi.basebill.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.basebill.domain.OmsOtherissuebillentry;
import com.ruoyi.basebill.mapper.OmsOtherissuebillMapper;
import com.ruoyi.basebill.domain.OmsOtherissuebill;
import com.ruoyi.basebill.service.IOmsOtherissuebillService;

/**
 * 其他出库单Service业务层处理
 * 
 * @author wenxyiong
 * @date 2023-02-18
 */
@Service
public class OmsOtherissuebillServiceImpl implements IOmsOtherissuebillService 
{
    @Autowired
    private OmsOtherissuebillMapper omsOtherissuebillMapper;

    @Autowired
    private OmsOtherissuebillServiceImpl otherissuebillService;

    /**
     * 查询其他出库单
     * 
     * @param id 其他出库单主键
     * @return 其他出库单
     */
    @Override
    public OmsOtherissuebill selectOmsOtherissuebillById(Long id)
    {
        return omsOtherissuebillMapper.selectOmsOtherissuebillById(id);
    }

    /**
     * 查询其他出库单列表
     * 
     * @param omsOtherissuebill 其他出库单
     * @return 其他出库单
     */
    @Override
    public List<OmsOtherissuebill> selectOmsOtherissuebillList(OmsOtherissuebill omsOtherissuebill)
    {
        return omsOtherissuebillMapper.selectOmsOtherissuebillList(omsOtherissuebill);
    }

    @Override
    public List<OmsOtherissuebill> selectOmsOtherissuebillDetailList(OmsOtherissuebill omsOtherissuebill) {
        return omsOtherissuebillMapper.selectOmsOtherissuebillDetailList(omsOtherissuebill);
    }

    /**
     * 新增其他出库单
     * 
     * @param omsOtherissuebill 其他出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertOmsOtherissuebill(OmsOtherissuebill omsOtherissuebill)
    {
        int rows = omsOtherissuebillMapper.insertOmsOtherissuebill(omsOtherissuebill);
        otherissuebillService.insertOmsOtherissuebillentry(omsOtherissuebill);
        return rows;
    }

    /**
     * 修改其他出库单
     * 
     * @param omsOtherissuebill 其他出库单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateOmsOtherissuebill(OmsOtherissuebill omsOtherissuebill)
    {
        //传入其他出库单不存在分录则不用修改分录信息只修改表头信息
        if(omsOtherissuebill.getOmsOtherissuebillentryList() != null && omsOtherissuebill.getOmsOtherissuebillentryList().size() != 0) {
            omsOtherissuebillMapper.deleteOmsOtherissuebillentryByParentid(omsOtherissuebill.getId());
            otherissuebillService.insertOmsOtherissuebillentry(omsOtherissuebill);
        }
        return omsOtherissuebillMapper.updateOmsOtherissuebill(omsOtherissuebill);
    }

    /**
     * 批量删除其他出库单
     * 
     * @param ids 需要删除的其他出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsOtherissuebillByIds(Long[] ids)
    {
        omsOtherissuebillMapper.deleteOmsOtherissuebillentryByParentids(ids);
        return omsOtherissuebillMapper.deleteOmsOtherissuebillByIds(ids);
    }

    /**
     * 删除其他出库单信息
     * 
     * @param id 其他出库单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteOmsOtherissuebillById(Long id)
    {
        omsOtherissuebillMapper.deleteOmsOtherissuebillentryByParentid(id);
        return omsOtherissuebillMapper.deleteOmsOtherissuebillById(id);
    }

    /**
     * 新增其他出库单明细信息
     * 
     * @param omsOtherissuebill 其他出库单对象
     */
    public void insertOmsOtherissuebillentry(OmsOtherissuebill omsOtherissuebill)
    {
        List<OmsOtherissuebillentry> omsOtherissuebillentryList = omsOtherissuebill.getOmsOtherissuebillentryList();
        Long id = omsOtherissuebill.getId();
        if (StringUtils.isNotNull(omsOtherissuebillentryList))
        {
            List<OmsOtherissuebillentry> list = new ArrayList<OmsOtherissuebillentry>();
            for (OmsOtherissuebillentry omsOtherissuebillentry : omsOtherissuebillentryList)
            {
                omsOtherissuebillentry.setParentid(id);
                list.add(omsOtherissuebillentry);
            }
            if (list.size() > 0)
            {
                omsOtherissuebillMapper.batchOmsOtherissuebillentry(list);
            }
        }
    }

    /**
     * 查询是否推送过EAS的其他出库单数据
     * @param isTrans
     * @return
     */
    @Override
    public List<OmsOtherissuebill> selectEasOtherissuebillServiceList(int isTrans) {
        return omsOtherissuebillMapper.selectEasOtherissuebillServiceList(isTrans);
    }
}
